package com.dayouzi.crawler_monitor.controller;


import com.dayouzi.crawler_monitor.base.result.R;
import com.dayouzi.crawler_monitor.base.result.ResponseEnum;
import com.dayouzi.crawler_monitor.pojo.page.PageBean;
import com.dayouzi.crawler_monitor.pojo.query.UserAdminQuery;
import com.dayouzi.crawler_monitor.pojo.vo.UserAdminVO;
import com.dayouzi.crawler_monitor.service.UserAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DaYouZi
 * @since 2023-03-06
 */

@Api(description = "管理员信息管理")
@Slf4j
@RestController
@RequestMapping("/crawler/admin")
public class UserAdminController {

    @Autowired
    private UserAdminService userAdminService;

    /**
     * 分页查询管理员信息list
     * @param page
     * @param limit
     * @param userAdminQuery
     * @return
     */
    @ApiOperation("分页查询管理员信息list")
    @PostMapping("/listPage/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Integer page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
            @ApiParam(value = "查询对象", required = true) @RequestBody UserAdminQuery userAdminQuery
            ) {
        PageBean<UserAdminVO> result = userAdminService.listPage(page, limit, userAdminQuery);
        return R.ok().data("result", result);
    }

    /**
     * 根据管理员id查询管理员信息
     * @param id
     * @return
     */
    @ApiOperation("根据管理员id查询管理员信息")
    @GetMapping("/getInfoById/{id}")
    public R getInfoById(
            @ApiParam(value = "管理员id", required = true) @PathVariable Integer id
    ) {
        UserAdminVO result = userAdminService.getInfoById(id);
        return R.ok().data("result", result);
    }


    /**
     * 根据管理员id永久删除管理员信息
     * @param id
     * @return
     */
    @ApiOperation("根据管理员id永久删除管理员信息")
    @GetMapping("/foreverRemove/{id}")
    public R foreverRemove(
            @ApiParam(value = "管理员id", required = true) @PathVariable Integer id
    ) {
        Boolean flag = userAdminService.foreverRemove(id);
        if (!flag) {
            return R.setResult(ResponseEnum.DELETE_FAILED);
        }
        return R.ok().code(200).message("删除成功");
    }

    /**
     * 根据管理员id修改管理员信息
     * @param userAdminQuery
     * @return
     */
    @ApiOperation("根据管理员id修改管理员信息")
    @PostMapping("/updateById")
    public R updateById(
            @ApiParam(value = "管理员信息对象", required = true) @Valid @RequestBody UserAdminQuery userAdminQuery
    ) {
        Boolean flag = userAdminService.updateInfoById(userAdminQuery);
        if (!flag) {
            return R.error().code(-213).message("修改失败");
        }
        return R.ok().code(200).message("修改成功");
    }

    /**
     * 新增管理员信息
     * @param userAdminQuery
     * @return
     */
    @ApiOperation("新增管理员信息")
    @PostMapping("/insertUserInfo")
    public R insertUserInfo(
            @ApiParam(value = "管理员信息对象", required = true) @Valid @RequestBody UserAdminQuery userAdminQuery
    ) {
        Boolean flag = userAdminService.insertUserInfo(userAdminQuery);
        if (!flag) {
            return R.error().code(-213).message("新增失败");
        }
        return R.ok().code(200).message("新增成功");
    }

    /**
     * 获取用户公告已读状态
     * @param userId
     * @return
     */
    @ApiOperation("获取用户公告已读状态")
    @GetMapping("/setNoticeStatus")
    public R setNoticeStatus(
            @ApiParam(value = "id", required = true) @RequestParam Integer userId
    ) {
        userAdminService.setNoticeStatus(userId);
        return R.ok();
    }

}

